﻿using System;
using System.Globalization;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Input;
using System.Windows.Media.Imaging;
namespace NewPanda
{
    class Converter
    {
    }

    public class MyConverter : IValueConverter
    {
        public object Convert(object o, Type type,
            object parameter, CultureInfo culture)
        {
            DateTime date = (DateTime)o;
            switch (type.Name)
            {
                case "String":
                    return date.ToString("F", culture);
                case "Brush":
                    return Brushes.Red;
                default:
                    return o;
            }
        }
        public object ConvertBack(object o, Type type,
            object parameter, CultureInfo culture)
        {
            return null;
        }
    }
    
    /// <summary>
    /// 该类负责将用浮点数标示的时间转换为
    /// 分:秒的形式
    /// </summary>
    public class DoubleDurationConverter : IValueConverter
    {
        public object Convert(object o, Type type, object paramter, CultureInfo culture)
        {
            double duration = (double)o;
            int mintues = ((int)duration) / 60;
            int seconds = ((int)duration) % 60;

            string s = "";
            if (mintues < 10)
            {
                s += "0" + mintues + ":";
            }
            else
            {
                s += mintues + ":";
            }

            if (seconds < 10)
                s += "0" + seconds;
            else
                s += seconds;
            
            return s;
        }

        public object ConvertBack(object o, Type type,
          object parameter, CultureInfo culture)
        {
            return null;
        }
    }



    /// <summary>
    /// 将字符串转化为图像;
    /// </summary>
    public class CoverConverter : IValueConverter
    {
        public object Convert(object o, Type type,
         object parameter, CultureInfo culture)
        {
            string source = (string)o;

            //<SnippetSimpleCSharp1>
            // Create the image element.
            Image simpleImage = new Image();
            simpleImage.Width = 40;
            simpleImage.Margin = new Thickness(5);

            // Create source.
            BitmapImage bi = new BitmapImage();
            // BitmapImage.UriSource must be in a BeginInit/EndInit block.
            bi.BeginInit();
            if (o == null || o.Equals(string.Empty))
            {
                bi.UriSource = new Uri(@"/Resource/Images/default_cover.png", UriKind.RelativeOrAbsolute);
            }
            else
            {
                bi.UriSource = new Uri(source, UriKind.RelativeOrAbsolute);
            }

            bi.EndInit();
            // Set the image source.
            simpleImage.Source = bi;


            return simpleImage;
        }


        public object ConvertBack(object o, Type type,
         object parameter, CultureInfo culture)
        {
            return null;
        }
    }

    public class FileSizeConverter : IValueConverter
    {
        public object Convert(object o, Type type, object paramter, CultureInfo culture)
        {
            long size = (long)o;
            double gb=((double)size)/Math.Pow(1024,3);
            double  mb = (size%Math.Pow(1024,3))/Math.Pow(1024,2);
            double kb = (size % Math.Pow(1024, 2)) / 1024;

            string s = "";
            if (gb >=1)
            {
                s = string.Format("{0:F}GB", gb);
            }
            else if (mb>=1)
            {
                s = string.Format("{0:F}MB", mb);
            }
            else
            {
                s = string.Format("{0:F}KB", kb);
            }

            return s;
        }

        public object ConvertBack(object o, Type type,
          object parameter, CultureInfo culture)
        {
            return null;
        }
    }
}
